package com.fly.server.handler;


import com.fly.server.vo.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName EntryPointUnauthorizedHandler
 * @Description 自定义认证异常
 * @Author Fly
 * @Date 2019/6/19 9:24
 */
@Component
public class EntryPointUnauthorizedHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException a) throws IOException, ServletException {
            response.setHeader("Authorization", "Bearer ");
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED, a.getMessage());
    }
}
